(function($) {
	var listDiv = $(".seckill-list");

//	var json = {
//		id : "11",
//		state : 0,// 0还没开始1 进行中2结束
//		img : "http://1.dms2015.sinaapp.com/dms_front/WebContent/static/dms/coupons/img/default.jpg",// 图片地址
//		text : "dd",// 内容
//		vantage : "100",//
//		endTime : "4",//
//		startTime : "5",// 状态为0的时候，还要传入endTime
//		count : "2"// 剩余数量
//	};
	var stateObj = {
		0 : [ "unstart", "离开始：", "startTime" ],
		1 : [ "", "离结束：", "endTime" ],
		2 : [ "end", "离结束：" ]
	};
	var liHtml = '<a href="#">'
			+ '<div class="banner-img">'
			+ '<span class="end"></span>'
			+ '<img src="">'
			+ '<i class="icon"></i>'
			+ '</div>'
			+ '<div class="cont clearfix">'
			+ '<div class="cont-r t_c">'
			+ '	<div class="price">'
			+ '		<p class="f_14">1000</p>'
			+ '		<p>积分</p>'
			+ '	</div>'
			+ '	<em>我要抢<i class="icon"></i></em>'
			+ '</div>'
			+ '<div class="cont-l">'
			+ '	<p>Nike下机新款100元优惠券限时抢购</p>'
			+ '	<p><span class="tickctState">离开始：</span><span class = "ticketDetails"><i>1</i>天<i>2</i>小时<i>4</i>分<i>56</i>秒</span><span class="pdl5">剩余：2000</span></p>'
			+ '</div>' + '</div>' + '</a>';

	function NavHeader(options) {
		this.active = 0;
		this.options = options || {url:"",param:""};
		this.lists = [];
		_initNavUI.call(this);
	}
	NavHeader.header = [ "进行中", "未开始", "已结束", " 所有" ];
	function _initNavUI() {
		this.nav = $("<nav/>").appendTo(listDiv);
		for ( var i = 0, len = NavHeader.header.length; i < len; i++) {
			this[i + "btn"] = $("<a href='javascript:;'>").html(
					NavHeader.header[i] + "<span></span>").appendTo(this.nav);
			this[i + "btn"].isLoad = false;
			this.lists[i] = new SeckillList();
			this.lists[i].hidden();
			this[i + "btn"].bind("click.nav",nav_click.bind(this,i));
		}

	}
	function nav_click (i){
		this.setActive(i);
	}
	
	NavHeader.prototype.setActive = function(index) {
		var options = this.options;
		this.lists[this.active].hidden();
		this[this.active + "btn"].removeClass("on");
		this.active = index;
		this[this.active + "btn"].addClass("on");
		if(this[index+ "btn"].isLoad == false){
			var data = options["param"][index];
			ajaxReq(options["url"],data,function(json){
			json = JSON.parse(json);
			this.lists[this.active].initByJson(json);
			this.lists[index].show();
			this[index + "btn"].isLoad = true;
			}.bind(this));
		}else{
			this.lists[index].show();
		}
	};

	
	function SeckillList() {
		this.listDom = $("<ul/>").appendTo(listDiv);
		this.show = function() {
			this.listDom.show();
		}.bind(this);
		this.hidden = function() {
			this.listDom.hide();
		}.bind(this);
	}

	SeckillList.prototype.addItem = function(ajson) {
		new Item(this, ajson);
	};
	
	SeckillList.prototype.initByJson = function(json){
		if(!$.isArray(json)) json = [json];
		for(var i =0,len=json.length;i<len;i++){
			this.addItem(json[i]);
		}
	};

	function Item(list, json) {
		this.list = list;
		this.json = json || {};
		this.state = this.json["state"];
		_initItemUI.call(this);
		_initByJson.call(this, this.json);
	}

	function _initItemUI() {
		this.itemDom = $("<li>").appendTo(this.list.listDom).html(liHtml)
				.addClass(stateObj[this.json["state"]][0]);
		this.itemDom.first().bind("click", item_click.bind(this));
	}

	function _initByJson(json) {
		this.img = $("img", this.itemDom).attr("src", json["img"]);
		this.vantage = $(".f_14", this.itemDom).text(json["vantage"]);
		this.text = $($(".cont-l", this.itemDom).children()[0]);
		this.text.text(json["text"]);
		this.count = $(".pdl5", this.itemDom).text("剩余:" + json["count"]);
		calculagraph.call(this, $(".ticketDetails", this.itemDom)[0]);
	}

	function item_click() {
		var tip = stateObj[this.state][1];
		alert(tip);
	}
	/**
	 * 倒计时逻辑
	 */
	function calculagraph(obj) {
		time = this.json[stateObj[this.state][2]];
		if(!time) time =0;
		date = parseInt(time, 10);
		var clearTime = null;
		var init = function(obj, date) {
			if (clearTime) {
				clearTimeout(clearTime);
				clearTime = null;
			}
			var span = obj.parentNode.firstChild;
			span.innerText = stateObj[this.state][1];
			var day = Math.floor(date / 24 / 60 / 60);
			var hours = Math.floor(date / 60 / 60) - day * 24;
			var minutes = Math.floor(date / 60) - day * 24 * 60 - hours * 60;
			var seconds = date - minutes * 60 - day * 24 * 60 * 60 - hours * 60
					* 60;
			date--;
			obj.innerHTML = '<i>' + day + '</i>天<i>' + hours + '</i>小时<i>'
					+ minutes + '</i>分<i>' + seconds + '</i>秒';
			if (date === -1) {
				var _obj = obj.parentNode.parentNode.parentNode.parentNode;
				if (_obj.parentNode.className == "end") {

				} else if (_obj.parentNode.className == "unstart") {
					setTimeout(function() {
						_obj.parentNode.className = "";
						this.state = "1";
					}.bind(this), 1000);
					date = parseInt(this.json["endTime"], 10);
					clearTime = setTimeout(function() {
						init.call(this, obj, date);
					}.bind(this), 1E3);
				} else {
					_obj.parentNode.className = "end";
					// _obj.setAttribute("href", "javascript:;");
				}

			} else {
				clearTime = setTimeout(function() {
					init.call(this, obj, date);
				}.bind(this), 1E3);
			}
		}
		init.call(this, obj, date);
	}

	function _start(options) {
		var header = new NavHeader(options);
		header.setActive(0);
	}

	function ajaxReq(url, data, sucfunc, sync) {
		$.ajax({
			type : "POST",
			url : url,
			data : data,
			async : sync ? sync : true,
			success : sucfunc
		});
	}
	;
	window["pageStart"] = _start;
})(jQuery);
